<?php
namespace app\index\model;
use think\Db;
use think\Model;

class Activity extends Model
{
	//查询活动
    public static function sel($where,$start,$limit,$uid){
    	 $activity=Db::name('activity')
    	           ->field('id as activity_id,name,time_from,time_to,location,price,enroll_num,lat,lng,display_member_id,goodsThums')
                 ->where($where)
                 ->order('id desc')
                 ->limit($start,$limit)
    	           ->select();
          if(empty($activity)){
            return $activity;
          }       
    	 foreach ($activity as $key => $value){
                 //查询报名的人     
                $actserId= Db::name('activity_users')->field('uids')
                         ->where('activity_id',$value['activity_id'])
                         ->where('uids','neq',$uid)
                         ->select(); 
    	           $activity[$key]['userList']=$actserId;
    	           }
        foreach ($activity as $key => $value){
           if(!empty($value['userList'])){
            $user_activity=array_column($value['userList'],'uids'); 
             $user_id=implode(',',$user_activity); 
             $userList= Db::name('user_info')->field('uid as  nearby_id,userPhoto,nickname')
                   ->where('uid','in',$user_id)
                   ->where(['member_type'=>'1'])
                   ->where('uid','neq',$uid)
                   ->select(); 
            $activity[$key]['userList']=array_replace($value['userList'],$userList);       
           }

        }
         return $activity;

    }
    //查询领队人气最高(关注量)的活动
    public static function popularity($where,$start,$limit){
       $fans=Db::name('user_info')
                 ->field('uid as id, max(fans) as fans')
                 ->where(['member_type'=>2])
                 ->select();
        if(empty($fans[0]['id'])){
            return  $fans[0]['id'];
       }            
       $captainFans=Db::name('user_info')
                   ->field('uid as id')
                   ->where(['fans'=>$fans[0]['fans'],'member_type'=>2])
                   ->find();          
        
        $id=$captainFans['id'];     
        $where=$where." and captain_id=$id";
        $activity=Db::name('activity')
                 ->field('id as activity_id,name,time_from,time_to,location,price,enroll_num,display_member_id,goodsThums')
                 // ->where($where)
                 ->where($where)
                 ->order('id desc')
                  ->limit($start,$limit)
                 ->select();        
        //return $activity;
        return $activity;
        
    }
     //查询领队评价最高(领队评分)的活动
    public static function evaluate($where,$start,$limit){
       $reta=Db::name('user_info')
                 ->field('uid as id, max(rate) as rate')
                 ->select();
        if(empty($reta[0]['id'])){
            return  $reta[0]['id'];
        }           
        $captainReta=Db::name('user_info')
                   ->field('uid as id')
                   ->where(['rate'=>$reta[0]['rate']])
                   ->find();
        $id=$captainReta['id'];               
        $where=$where." and captain_id=$id";                               
       $activity=Db::name('activity')
                 ->field('id as activity_id,name,time_from,time_to,location,price,enroll_num,display_member_id,goodsThums')
                 ->where($where)
                 ->order('id desc')
                 ->limit($start,$limit)
                 ->select();        
         return $activity;
    }  
    //查询轮播图
    public static function banner(){
    	$data=Db::name('ads')->field('adFile')->limit(3)->select();
    	return $data;
    }
    //活动劵
    public static function discounts(){
       $activity_discounts=Db::name('activity')
                 ->field('id as securitId,name,time_from,time_to,location,preferential_price,goodsThums')
                 ->where(['activity_state'=>1])
                 ->select();
        return $activity_discounts;       

    }
    //活动评价
    public static function reviewSet($data){
         $appadd=Db::name('activity_appraise')
                 ->insert($data);
        return $appadd; 
    }
    //判断用户是否评价过
    public static function reviewSel($uid,$activity_id){
         $appraise=Db::name('activity_appraise')
                 ->where(['uid'=>$uid,'activity_id'=>$activity_id])
                 ->find();
        return $appraise; 
    } 
    //活动劵添加
    public static function discounts_add($data){
       $activity_discounts=Db::name('user_securities')
                 ->insert($data);
        return $activity_discounts;       

    }
    //活动详情
    public static function actDetails($activity_id,$uid){
        $actOne=Db::name('activity')
    	           ->where(['id'=>$activity_id])
    	           ->find();
         $nowtime=time();//现在时间 
         $createTime=$actOne['createTime'];
         if($nowtime>$createTime){
            $upTo=-1;//已截至
         }else{
          $upTo=1;//可报名
         }  
         //查询报名的人     
          $actserId= Db::name('activity_users')->field('uids')
                   ->where('activity_id',$activity_id)
                   ->select(); 
          if(empty($actserId)){
            $userList=[];
          }else{
             $user_activity=array_column($actserId,'uids'); 
             $user_id=implode(',',$user_activity); 
             $userList= Db::name('user_info')->field('uid as playmate_id,userPhoto,nickname')
                   ->where('uid','in',$user_id)
                   ->where(['member_type'=>'1'])
                   ->where('uid','neq',$uid)
                   ->select();  
          }               
           $captain= Db::name('user_info')
                   ->field('userPhoto as captainPhoto,nickname as name,rate,fans,actNum,uid as captain_id')  
                   ->where('uid',$actOne['captain_id'])
                   ->select(); 
           //查询用户评价
            $activity_appraise=Db::name('activity_appraise') 
                          ->field('uid as nearby_id ,content,nickname,userPhoto')  
                          ->where('activity_id',$activity_id)
                          ->select();
           $actOne['upTo']=$upTo;                                
           $actOne['userList']=$userList;                     
           $actOne['activity_appraise']=$activity_appraise;                                    
           $actOne['captain']=$captain;                         
         return $actOne;
    }
    //活动玩伴列表
    public static function actPlaymate($activity_id){
          $playmateList = Db::name('activity_users')->alias('au')->join('user_info u','au.uids = u.uid','left')->where(['activity_id'=>$activity_id])->field('uid as nearby_id,nickname,userPhoto,university,gender,city,constellation,birthday,char_length(sef_desc) as sef_desc')->select();
            return $playmateList;       
    }
    public static function actPlay($activity_id,$uid){
        $playmateList = Db::name('activity_users')->alias('au')->join('user_info u','au.uids = u.uid','left')->where("activity_id=$activity_id and uid!=$uid")->field('uid as nearby_id,nickname,userPhoto,university,gender,city,longitude,latitude,constellation,birthday,char_length(sef_desc) as sef_desc')->select();
            return $playmateList;
    }
    //领队个人详情
    public static function actCaptain($captain_id){
           $captainOne = Db::name('user_info')
                   ->field('uid as captain_id,nickname as name,fans,rate,actNum,attention,userImg1,userImg2,userImg3,userImg4,userImg5')
                  ->where(['uid'=>$captain_id,'member_type'=>2])->find();
            if(empty($captainOne)){
              return $captainOne;
            }      
          $userAtivity=Db::name('activity')->field('id as activity_id,name,time_from,time_to,location,goodsThums')->where(['captain_id'=>$captain_id])->select();
          //重组图片
            $imgs=[$captainOne['userImg1'],$captainOne['userImg2'],$captainOne['userImg3'],$captainOne['userImg4'],$captainOne['userImg5']];
                //print_r($imgs);die;
                $imgList=array();
                foreach ($imgs as $k => $img) {
                    if(!empty($img)){
                        $imgList[]=$img;
                    }
                }
                
                $captainOne['ImgList']=$imgList;
          // $captainOne['userAtivity']=$userAtivity;
            return $captainOne;       
    }
     //领队发布的活动
    public static function actRelease($captain_id,$start,$limit){
          $userAtivity=Db::name('activity')->field('id as activity_id,name,time_from,time_to,location,goodsThums')->where(['captain_id'=>$captain_id])->limit($start,$limit)->select();
            return $userAtivity;       
    }
    //领队活动评价
    public static function appraise($captain_id,$start,$limit){
          $appraise=Db::name('activity_appraise')->field('uid as nearby_id,content,nickname,userPhoto')->where(['captain_id'=>$captain_id])->limit($start,$limit)->select();
            return $appraise;    
    }
    //我参加的活动
    public static function Myactivity($uid,$start,$limit){
         //查询参加的活动id    
          $activity_users=Db::name('activity_users')
                   ->field('activity_id')
                   ->where(['uids'=>$uid])
                   ->limit($start,$limit)
                   ->select(); 
            if(empty($activity_users)){
              return $activity_users;
            }   
                
           $activity=array_column($activity_users,'activity_id'); 
           $activity_id=implode(',',$activity);      
         /**/   
         //获取参加的活动详情
         $activityList=Db::name('activity')
                   ->where('id','in',$activity_id)
                   ->field('id as activity_id,name,time_from,time_to,location,goodsThums')
                   ->select();
          if(!empty($activityList)){
             foreach($activityList as $k=>$val){
               //查询我的评价
            $re=Db::name('activity_appraise')->where(['uid'=>$uid,'activity_id'=>$val['activity_id']])->find(); 
            if($re){
              $activityList[$k]['luate']='1';
            }else{
               $activityList[$k]['luate']='-1';
            }   
            } 
          }    
         return $activityList;          
    }
    //活动搜索
    public static function activitySet($activityName){
       $where="name".' '.'like'.' '."'".'%'.$activityName.'%'."'";
        $actShow=Db::name('activity')
                   ->where($where)
                   ->where(['checkId'=>'1'])
                   ->field(' id as activity_id,name,time_from,time_to,location,price,enroll_num,lat,lng,display_member_id,goodsThums')
                   ->select();
         return $actShow;  
    }


}
